package com.shz.controller;


import com.shz.pojo.ChatMsg;
import com.shz.pojo.User;
import com.shz.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class WebSocketController {
    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;
    @Resource
    private UserService userService;
//    @Resource
//    OrderService orderService;
    Logger logger = LoggerFactory.getLogger(WebSocketController.class);
    /**
     * 处理消息
     *
     * @param authentication 身份验证
     * @param chatMsg        聊天消息
     */
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @MessageMapping("/ws/success")
    public void success(Authentication authentication,ChatMsg c) {
        User u = (User) authentication.getPrincipal();
        c.setFrom(u.getUsername());
        c.setFromNickName(u.getUsername());
        System.out.println(c);
        System.out.println("success");
        simpMessagingTemplate.convertAndSendToUser(c.getTo(),"/queue/chat",c);
    }


    @MessageMapping("/ws/sendAll")
    public void sendSign(Authentication authentication,ChatMsg c) {
        User u = (User) authentication.getPrincipal();
        c.setFrom(u.getUsername());
        c.setFromNickName(u.getUsername());
        System.out.println("success");
        simpMessagingTemplate.convertAndSendToUser(c.getTo(),"/queue/sign",c);
    }



//
//    @MessageMapping("/ws/chat")
//    public void handleMsg(Authentication authentication, ChatMsg chatMsg){
//        User u = (User) authentication.getPrincipal();
//        chatMsg.setFrom(u.getUsername());
//        chatMsg.setFromNickName(u.getUsername());
//        chatMsg.setDate(LocalDateTime.now());
//        logger.info(JSONUtil.toJsonStr(chatMsg));
//        chatMsg.setSelf(true);
//        stringRedisTemplate.opsForList().rightPush("sessionBackList:"+chatMsg.getFrom(),JSONUtil.toJsonStr(chatMsg));
//        ConcurrentHashMap<String, Friend> map = FriendStatusController.getMap();
//        chatMsg.setSelf(false);
//        stringRedisTemplate.opsForList().rightPush("sessionBackList:"+chatMsg.getTo(),JSONUtil.toJsonStr(chatMsg));
//        if(!map.containsKey(chatMsg.getTo())){
//            stringRedisTemplate.opsForList().rightPush("sessionList:"+chatMsg.getTo(),JSONUtil.toJsonStr(chatMsg));
//            return;
//        }
//
//        /**
//         * 发送消息
//         * 1.消息接收者
//         * 2.消息队列
//         * 3.消息对象
//         */
//        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);
//    }
//
//
//    @MessageMapping("/ws/reminder")
//    public void handleMsgReminder(Authentication authentication, ChatMsg chatMsg){
//        User u = (User) authentication.getPrincipal();
//        chatMsg.setFrom(u.getUsername());
//        chatMsg.setFromNickName(u.getUsername());
//        chatMsg.setDate(LocalDateTime.now());
//        chatMsg.setContent("用户催单请及时处理！！ ");
//        Order order = chatMsg.getOrder();
//        orderService.update(new UpdateWrapper<Order>().eq("id",order.getId()).set("reminder",true));
//        List<User> list = userService.query().eq("roleName", 2).list();
//        for (User user : list) {
//            chatMsg.setTo(user.getUsername());
//            simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);
//        }
//        logger.info(JSONUtil.toJsonStr(chatMsg));
//
//        /**
//         * 发送消息
//         * 1.消息接收者
//         * 2.消息队列
//         * 3.消息对象
//         */
//    }






}